package com.example.owner.lettery1.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.owner.lettery1.R;

public class NewsFragment extends BackHandledFragment {

    public static NewsFragment newInstance() {
        return new NewsFragment();
    }


    private WebView webView;
    private WebSettings webSettings;

    private String url = "https://m.qmcai.com/zixun/index.html?h5ControlTitle=true&backH5Control=true";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news,container,false);
        webView = (WebView)view.findViewById(R.id.web_view_news);

        webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);

        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl(url);

        return view;
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //    return super.shouldOverrideUrlLoading(view, url);
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            //     super.onReceivedError(view, errorCode, description, failingUrl);
            //  Toast.makeText(this,"网页加载错误！",0).show();
        }
    }

    @Override
    public boolean onBackPressed() {
        if(webView.canGoBack()){
            webView.goBack();
            Log.v("webView.goBack()", "webView.goBack()");
            return true;

        }else{
            Log.v("Conversatio退出","Conversatio退出");
            return false;
        }

    }


   /* @BindView(R.id.web_view_news)
    WebView webViewNews;
    Unbinder unbinder;
    @BindView(R.id.title_view_news)
    TextView titleViewNews;
    Unbinder unbinder1;

    public static NewsFragment newInstance() {
        return new NewsFragment();
    }


    @Override
    protected void onInitView(View view, Bundle savedInstanceState) {
        WebSettings settings = webViewNews.getSettings();
        settings.setJavaScriptEnabled(true);

        //支持缩放
        settings.setUseWideViewPort(true);//设定支持viewport
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);//设定支持缩放

        //打开网址
        webViewNews.loadUrl("https://m.qmcai.com/zixun/index.html?h5ControlTitle=true&backH5Control=true");
        webViewNews.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
            
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news;
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder1.unbind();
    }*/
}
